package com.zengqingfa.springboot.validation.demo.validator;

import com.zengqingfa.springboot.validation.demo.annotation.IsMobile;
import com.zengqingfa.springboot.validation.demo.util.ValidatorUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author zengqingfa
 * @date 2019/11/27 17:02
 * @description
 * @email zengqingfa_java@163.com
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

    private boolean required = false;

    //初始化方法可以拿到注解
    @Override
    public void initialize(IsMobile isMobile) {
        System.out.println("hello isMobile");
        required = isMobile.required();
    }

    //校验方法
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        System.out.println("hello is mobile  is valid");
        if (required) {
            return ValidatorUtil.isMobile(value);
        } else {
            if (value == "" || value == null) {
                return true;
            } else {
                return ValidatorUtil.isMobile(value);
            }
        }
    }
}
